#include"../log/log.h"
void test1()//测试util.hpp内的小组件
{
    std::cout<<logs::util::Date::now()<<std::endl;
    std::cout<<logs::util::file::pathname("/home/hay/2.12.work")<<std::endl;
    logs::util::file::CreateDreactor("./test/test.c");
    logs::util::file::CreateDreactor("test2.c");
}
void test2()//测试util.hpp内的小组件
{
    std::cout<<logs::loglevel::to_String(logs::loglevel::level::DEBUG)<<std::endl;
    std::cout<<logs::loglevel::to_String(logs::loglevel::level::ERROR)<<std::endl;
    std::cout<<logs::loglevel::to_String(logs::loglevel::level::FATAL)<<std::endl;
    std::cout<<logs::loglevel::to_String(logs::loglevel::level::INFO)<<std::endl;
    std::cout<<logs::loglevel::to_String(logs::loglevel::level::UNKNOW)<<std::endl;
    std::cout<<logs::loglevel::to_String(logs::loglevel::level::WARN)<<std::endl;
    std::cout<<logs::loglevel::to_String(logs::loglevel::level::OFF)<<std::endl;
}
void test3()//测试日志消息类和格式化类
{
    logs::LogMsg msg(logs::loglevel::level::DEBUG,__FILE__,"日志器1","测试日志",__LINE__);
    // logs::Formater ft;
    // std::cout<<ft.format(msg);
      logs::Formater ft("[%d{%H:%M:%S}[%c][%m]%n]");//此时我们可以发现,只有我们输入了对应的格式字符,日志消息才会输出对应的部分
    std::cout<<ft.format(msg);
}
void test4()//测试日志消息落地类
{
    logs::LogMsg msg(logs::loglevel::level::DEBUG,__FILE__,"日志器1","测试日志",__LINE__);
    logs::Formater ft;
    std::string ret = ft.format(msg);//此时是把消息准备好了
    //下面是解决消息的输出方向
    auto sptr = logs::Factorysink::Create<logs::Stdoutsink>();
    sptr->log( ret.c_str(),ret.size());
    auto sptr2 = logs::Factorysink::Create<logs::Filesink>("./logs/test.log");
   sptr2->log( ret.c_str(),ret.size());
   auto sptr3 = logs::Factorysink::Create<logs::Rollsink>("./test/test.log",1024);
   int i = 0;
   while(i < 5*1024)
   {
        i += ret.size();
        sptr3->log(ret.c_str(),ret.size());
   }
}

void test6()//测试日志器功能
{
    logs::loglevel::level lev = logs::loglevel::level::DEBUG;
    logs::Formater::ptr ft(new logs::Formater());
    auto sptr1 = logs::Factorysink::Create<logs::Stdoutsink>();
    auto sptr2 = logs::Factorysink::Create<logs::Filesink>("./logs/test.log");
    auto sptr3 = logs::Factorysink::Create<logs::Rollsink>("./test/test.log",1024);
    std::vector<logs::sink::ptr> vp{sptr1,sptr2,sptr3};
    std::string logname = "同步日志器";
    logs::sync_Logger sl(logname,vp,lev,ft);
    sl.Debug(__FILE__,__LINE__,"日志测试");

    // sl.Info(__FILE__,__LINE__,"日志测试");
    // sl.Error(__FILE__,__LINE__,"日志测试");
    // sl.War(__FILE__,__LINE__,"日志测试");
    // sl.Fatal(__FILE__,__LINE__,"日志测试");
    // sl.Off(__FILE__,__LINE__,"日志测试");
   int i = 0;
   while(i < 5*1024)
   {
        i+=78;
        sl.Debug(__FILE__,__LINE__,"日志测试");
   }
}
//日志建造者测试
void test7()
{
    std::shared_ptr<logs::Localloggerbuild> localbuild(new(logs::Localloggerbuild));
    localbuild->buildlevel(logs::loglevel::level::DEBUG);
    localbuild->buildformater("%d%m%n");
    localbuild->buildlogname("日志器1");
    localbuild->buildsink<logs::Filesink>("./logs/test.log");
    localbuild->buildsink<logs::Rollsink>("./logs/test.log",1024);
    logs::Logger::ptr lptr = localbuild->build();
    int i = 0;
   while(i < 5*1024)
   {
        i += 21;
        lptr->Debug(__FILE__,__LINE__,"日志测试");
   }
}
void test8()//测试缓冲区模块
{
    logs::buffer buf;
    std::ifstream ifs;
    ifs.open("./logs/test.log",std::ifstream::binary);
    ifs.seekg(0,std::ifstream::end);
    size_t size = ifs.tellg();
    std::string rec;
    rec.resize(size);
    ifs.seekg(0,std::ifstream::beg);
    ifs.read(&rec[0],size);//将文件数据读取到rec字符串中
    
    buf.push(rec.c_str(),rec.size());//写入缓冲区
    // buf.movewriter(rec.size());
    std::cout<<buf._writer_index<<std::endl;
    std::ofstream ofs("./logs/test2.log",std::ofstream::binary);//从缓冲区读数据到文件
    int readsize = buf.ReadAbleSize();
    for(int i = 0; i < size;i++)
    {
        ofs.write(buf.begin(),1);
        buf.moveReader(1);
    }//最后我们在外部用命令比较两文件是否一致
}
void test9()//测试异步日志器"[%d{%H:%M:%S}[%c][%m]%n]"
{
    std::shared_ptr<logs::Localloggerbuild> localbuild(new(logs::Localloggerbuild));
    localbuild->buildlevel(logs::loglevel::level::DEBUG);
    localbuild->buildformater("%d{%H:%M:%S}[%m]%n");
    localbuild->buildlogname("日志器1");
    localbuild->buildloggertype(logs::LoggerType::Asynclooper);
    localbuild->buildsink<logs::Filesink>("./logs/test2.log");
    localbuild->buildsink<logs::Rollsink>("./logs/test2.log",1024);
    logs::Logger::ptr lptr = localbuild->build();
    int i = 0;
    int count = 0;
   while(i < 5*1024)
   {
        i += 21;
        lptr->Debug(__FILE__,__LINE__,"日志测试");
   }
}
int _t = 0;
void test10()
{
    std::mutex _mutex;
    // std::unique_lock<std::mutex>(_mutex);
    std::unique_lock<std::mutex> lock(_mutex); // 加锁访问缓冲区
    std::condition_variable _pro_condition;
    // _pro_condition.wait(_mutex);
    _pro_condition.wait(lock,[&](){return false;});
}
void test_log()//测试获取全局的日志器
{
    logs::Logger::ptr manger = logs::LoggerManager::getinstance().getlogger("日志器1");
    int i = 0;
    int count = 0;
   while(i < 5*1024)
   {
        i += 21;
        manger->Debug(__FILE__,__LINE__,"日志测试");
   }

}
void test11()//测试日志管理模块
{
    std::shared_ptr<logs::Globalloggerbuild> localbuild(new(logs::Globalloggerbuild));
    localbuild->buildlevel(logs::loglevel::level::DEBUG);
    localbuild->buildformater("%d{%H:%M:%S}[%m]%n");
    localbuild->buildlogname("日志器1");
    localbuild->buildloggertype(logs::LoggerType::Asynclooper);
    localbuild->buildsink<logs::Filesink>("./logs/test2.log");
    localbuild->buildsink<logs::Rollsink>("./logs/test2.log",1024);
    localbuild->build();//建造日志器并添加到管理对象中
    test_log();
}
void test2_log()//测试全局接口
{
    logs::Logger::ptr manger = logs::getlogger("日志器1");//获取日志器
    manger->Debug("%s","日志测试");
    manger->Info("%s","日志测试");
    manger->War("%s","日志测试");
    manger->Error("%s","日志测试");
    manger->Fatal("%s","日志测试");
    manger->Off("%s","日志测试");
    DEBUG("%s","日志测试");
    INFO("%s","日志测试");
    WAR("%s","日志测试");
    ERROR("%s","日志测试");
    FATAL("%s","日志测试");
    OFF("%s","日志测试");
}
//下面是代码最终的使用样例
void test12()
{
    std::shared_ptr<logs::Globalloggerbuild> localbuild(new(logs::Globalloggerbuild));//创建一个全局建造者
    //建造日志器对象成员
    localbuild->buildlevel(logs::loglevel::level::DEBUG);
    localbuild->buildformater("%d{%H:%M:%S}[%f:%l][%m]%n");
    localbuild->buildlogname("日志器1");
    localbuild->buildloggertype(logs::LoggerType::Asynclooper);
    localbuild->buildsink<logs::Filesink>("./logs/test2.log");
    localbuild->buildsink<logs::Stdoutsink>();
    localbuild->buildsink<logs::Rollsink>("./logs/test.log",1024);
    //组装后返回日志器,全局建造者返回的是全局日志器
    logs::Logger::ptr lptr = localbuild->build();
    //测试在全局范围使用该日志器
    test2_log();
}
int main()
{
    test12();
    
    return 0;
}